<?php
class Article_Admin extends CI_Controller {
	function __construct() {
		parent::__construct();
		$this->load->model('volunteer_model');
		$this->load->model('article_model');
		if(!$this->session->userdata('vol_id')) {
			redirect('admin/index/login_timeout', 'refresh');
		}
	}

	function add_art($status = NULL) {
		if($status == 'submit') {
			$post = $this->input->post();
			$post['input'] = $this->session->userdata('vol_id');
			//if(isset($post['do'])) unset($post['do']);
			if($this->article_model->add_art($post) == 0) {			
				$result['status'] = '您的文章提交成功！';
				$result['redirect'][] = array('link' => 'article_admin/add_art', 'title' => '继续编辑');
				$result['redirect'][] = array('link' => 'article_admin/art_list', 'title' => '返回文章列表');
			}
			else {
				$result['status'] = '对不起，由于数据库错误，提交失败。';
				$result['redirect'][] = array('link' => 'article_admin/add_art', 'title' => '返回重试');
				$result['redirect'][] = array('link' => 'article_admin/art_list', 'title' => '返回文章列表');
			}
			$this->load->view('status', $result);
		}
		else {
			$art_post = array('title' => '', 'type' => 1, 'content' => '', 'submit' => 'article_admin/add_art/submit');
			$this->load->view('article_post', $art_post);
		}
	}

	function edit_art($art_id, $status = NULL) {
		if($status == 'submit') {
			$post = $this->input->post();
			$post['input'] = $this->session->userdata('vol_id');
			//if(isset($post['do'])) unset($post['do']);
			if($this->article_model->edit_art($art_id, $post) == 0) {			
				$result['status'] = '您的文章提交成功！';
				$result['redirect'][] = array('link' => 'article_admin/art_list', 'title' => '返回文章列表');
			}
			else {
				$result['status'] = '对不起，由于数据库错误，提交失败。';
				$result['redirect'][] = array('link' => 'article_admin/art_list', 'title' => '返回文章列表');
			}
			$this->load->view('status', $result);
		}
		else {
			$art_post = $this->article_model->get_full_art($art_id);
			$art_post['submit'] = 'article_admin/edit_art/'.$art_id.'/submit';
			$this->load->view('article_post',$art_post);
		}
	}

	function art_list() {
		$art_list['record'] = $this->article_model->get_art_list();
		$this->load->view('article_manager', $art_list);
	}

	function art_delete() {

	}

	function batch_edit() {

	}

	function batch_delete() {
		$art_id = $this->input->post('art_id');
		$count = count($art_id);
		$error = 0;
		foreach($art_id as $key) {
			if($this->article_model->delete_art($key) == -1) $error++;
		}
		if($error > 0) {
			$result['stauts'] = "选中的".$count."篇文章共有".$error."篇未能成功删除。";
		}
		else {
			$result['status'] = "选中的".$count."篇文章已成功删除。";
		}
		$result['redirect'][] = array('link' => 'article_admin/art_list', 'title' => '返回文章列表');
		$this->load->view('status', $result);
	}
}
/* End of file article_admin.php */